package com.example.myapplication2;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class fragment_MainActivity extends AppCompatActivity implements View.OnClickListener{
    // 定义变量
    Fragment fragment1,fragment2,fragment3,fragment4;
    LinearLayout layout1,layout2,layout3,layout4;
    FragmentManager manager;
    FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_main);

        // 初始化四个LinearLayout
        layout1=findViewById(R.id.bottom_LinearLayout1);
        layout2=findViewById(R.id.bottom_LinearLayout2);
        layout3=findViewById(R.id.bottom_LinearLayout3);
        layout4=findViewById(R.id.bottom_LinearLayout4);

        // 初始化四个Fragment
        fragment1=new Fragment1();
        fragment2=new Fragment2();
        fragment3=new Fragment3();
        fragment4=new Fragment4();

        // 获取FragmentManager并开始一个事务
        manager=getSupportFragmentManager();
        transaction=manager.beginTransaction();

        // 调用initial方法添加所有Fragment到FrameLayout
        intial();

        // 调用fragementHide方法隐藏所有Fragment
        fragementHide();

        // 显示第一个Fragment
        transaction.show(fragment1);
        transaction.commit();

        // 为每个LinearLayout设置点击监听器，点击时显示对应的Fragmen
        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction=manager.beginTransaction();
                fragementHide();
                transaction.show(fragment1);
                transaction.commit();
            }
        });

        layout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction=manager.beginTransaction();
                fragementHide();
                transaction.show(fragment2);
                transaction.commit();
            }
        });

        layout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction=manager.beginTransaction();
                fragementHide();
                transaction.show(fragment3);
                transaction.commit();
            }
        });

        layout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction=manager.beginTransaction();
                fragementHide();
                transaction.show(fragment4);
                transaction.commit();
            }
        });
    }

    // 初始化方法：将四个Fragment添加到FrameLayout中
    private void intial() {
        transaction.add(R.id.framelayout,fragment1);
        transaction.add(R.id.framelayout,fragment2);
        transaction.add(R.id.framelayout,fragment3);
        transaction.add(R.id.framelayout,fragment4);
    }

    // 隐藏所有Fragment的方法
    void fragementHide() {
        transaction.hide(fragment1);
        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.hide(fragment4);
    }

    // 实现View.OnClickListener接口的onClick方法
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.bottom_LinearLayout1) {
            fragementHide();
            showfragment(fragment1);
        } else if (view.getId() == R.id.bottom_LinearLayout2) {
            fragementHide();
            showfragment(fragment2);
        } else if (view.getId() == R.id.bottom_LinearLayout3) {
            fragementHide();
            showfragment(fragment3);
        } else if (view.getId() == R.id.bottom_LinearLayout4) {
            fragementHide();
            showfragment(fragment4);
        } else {}
    }

    // 显示指定Fragment的方法
    private void showfragment(Fragment fragment) {
        transaction=manager.beginTransaction();
        fragementHide();
        transaction.show(fragment);
        transaction.commit();
    }
}